package com.tencent.welink.iot;

import com.tencent.welink.util.HttpPoolUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.ParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 消息拉取
 * @Author: limye
 * @CreateDate: 2020-6-9
 * @Version: V1.0
 */
public class WelinkHttpPull {

    private static final Logger log = LogManager.getLogger(WelinkHttpPull.class);

    //头部参数
    private Map<String, Object> headerPara = new HashMap<>();

    public Map<String, Object> getHeaderPara() {
        return headerPara;
    }

    /**
     * 获取请求头
     *
     * @return
     */
    private Header[] getHeader() {
        Header[] headers = null;
        if (!headerPara.isEmpty()) {
            headers = new Header[headerPara.size()];
            int i = 0;
            for (Map.Entry entry : headerPara.entrySet()) {
                headers[i++] = new Header() {
                    @Override
                    public String getName() {
                        return (String) entry.getKey();
                    }

                    @Override
                    public String getValue() {
                        return (String) entry.getValue();
                    }

                    @Override
                    public HeaderElement[] getElements() throws ParseException {
                        return new HeaderElement[0];
                    }
                };
            }
        }
        return headers;
    }


    public byte[] doResultByHttpGet(String url) {
        try {
            byte[] result = HttpPoolUtils.httpGetByteArray(url, getHeader());
            return result;
        } catch (Exception e) {
            log.error(e);
        }
        return null;
    }

}
